/**
 * MINAPL Compiler.
 * Copyright 2012 Michael A. Muller
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */


#include "Token.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace minapl;

void Token::dump() {
    switch (type) {
        case TOK_END:
            printf("END\n");
            break;
        case TOK_ID:
            printf("ID ");
            fwrite(text, size, 1, stdout);
            printf("\n");
            break;
        case TOK_STR:
            printf("STR '");
            fwrite(text, size, 1, stdout);
            printf("'\n");
            break;
        case TOK_SEMI:
            printf("SEMI\n");
            break;
        default:
            fprintf(stderr, "Invalid token type %d", type);
            abort();
    }
}

int Token::repLen() {
    switch (type) {
        case TOK_END:
            return 3;
        case TOK_ID:
            return size;
        case TOK_STR:
            return 2 + size;
        case TOK_INT:
            return 10;
        case TOK_SEMI:
        case TOK_LCURLY:
        case TOK_RCURLY:
        case TOK_LPAREN:
        case TOK_RPAREN:
        case TOK_DOT:
            return 1;
    }
}

void Token::toRep(char *buffer) {
    switch (type) {
        case TOK_END:
            strcpy(buffer, "END");
            break;
        case TOK_ID:
            strncpy(buffer, text, size);
            buffer[size] = 0;
            break;
        case TOK_STR:
            sprintf(buffer, "'%s'", text);
            break;
        case TOK_INT:
            sprintf(buffer, "%d" , size);
            break;
        case TOK_SEMI:
            buffer[0] = ';'; buffer[1] = 0;
            break;
        case TOK_LCURLY:
            buffer[0] = '{'; buffer[1] = 0;
            break;
        case TOK_RCURLY:
            buffer[0] = '}'; buffer[1] = 0;
            break;
        case TOK_LPAREN:
            buffer[0] = '('; buffer[1] = 0;
            break;
        case TOK_RPAREN:
            buffer[0] = ')'; buffer[1] = 0;
            break;
        case TOK_DOT:
            buffer[0] = '.'; buffer[1] = 0;
            break;
    }
}

// Copy the text of the token and return it as an allocated null
// terminated string.
char *Token::copyText() {
    char *result = strndup(text, size);
    result[size] = 0;
    return result;
}
